﻿Imports System
Imports System.IO
Imports System.Reflection
Imports System.Xml.Serialization
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Security.Permissions
Imports System.Collections
Public Class Serializer
    Public Sub SerializeToFile(Of T)(ByVal path As String, ByVal data As T)
        'open a file stream to save our object in.
        Using fs As New FileStream(path, FileMode.Create)

            'create a formatter for this file stream
            Dim bf As New BinaryFormatter(Nothing, _
            New StreamingContext(StreamingContextStates.File))

            'serialize the object and close the stream
            bf.Serialize(fs, data)

        End Using
    End Sub

    Public Function DeserializeFromFile(Of T)(ByVal path As String) As T
        'open the stream from input path
        Using fs As New FileStream(path, FileMode.Open)

            'create a formatter for this file stream
            Dim bf As New BinaryFormatter(Nothing, _
            New StreamingContext(StreamingContextStates.File))

            'deserialize the object from the stream
            Return DirectCast(bf.Deserialize(fs), T)

        End Using
    End Function
End Class
